package cn.thornbird.orgsync.helper.converter;

import cn.thornbird.orgsync.helper.AppHelper;
import cn.thornbird.orgsync.model.AppDepartmentData;
import cn.thornbird.orgsync.model.AppUserData;
import cn.thornbird.orgsync.model.dingtalk.DepartmentBaseList;
import cn.thornbird.orgsync.model.dingtalk.UserInfo;
import cn.thornbird.orgsync.model.dingtalk.UserList;

import java.util.HashMap;

public class DingTalkConverter extends DataConverter<DepartmentBaseList.DepartmentBase, UserList.User, UserInfo> {

    @Override
    public AppDepartmentData parseDepartment(DepartmentBaseList.DepartmentBase department) {
        AppDepartmentData departmentData = new AppDepartmentData();
        departmentData.setId(department.getDeptId().toString());
        departmentData.setParentId(department.getParentId() == null ? null : department.getParentId().toString());
        departmentData.setName(department.getName());
        return departmentData;
    }

    @Override
    public AppUserData parseUser(UserList.User user) {
        if (user == null) {
            return null;
        }

        AppUserData userData = new AppUserData();
        userData.setId(user.getUserId());
        userData.setEmployeeNo(user.getJobNumber());
        userData.setName(user.getName());
        userData.setAvatar(user.getAvatar());
        userData.setMobile(user.getMobile());
        userData.setEmail(user.getEmail());
        userData.setTelephone(user.getTelephone());
        userData.setTitle(user.getTitle());
        userData.setLeaderId(user.getManagerUserId());

        int count = user.getDeptIdList() == null ? 0 : user.getDeptIdList().size();
        if (user.getDeptOrderList() != null && user.getLeaderInDept() != null) {
            count = Math.min(count, Math.min(user.getDeptOrderList().size(), user.getLeaderInDept().size()));
        }
        if (count > 0) {
            userData.setDepartments(new HashMap<>());
            for (int i = 0; i < count; i++) {
                String id = user.getDeptIdList().get(i).toString();
                Long order = user.getDeptOrderList().get(i).getOrder();
                Boolean leader = user.getLeaderInDept().get(i).getLeader();
                AppUserData.DepartmentInfo departmentInfo = new AppUserData.DepartmentInfo();
                departmentInfo.setOrder(order);
                departmentInfo.setLeader(leader);
                userData.getDepartments().put(id, departmentInfo);
            }
        }

        return userData;
    }

    @Override
    public UserInfo toUserInfo(AppUserData userData) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userData.getId());
        userInfo.setJobNumber(userData.getEmployeeNo());
        userInfo.setName(userData.getName());
        userInfo.setAvatar(userData.getAvatar());
        userInfo.setMobile(AppHelper.getPureMobile(userData.getMobile()));
        userInfo.setEmail(userData.getEmail());
        userInfo.setTelephone(userData.getTelephone());
        userInfo.setTitle(userData.getTitle());
        userInfo.setManagerUserId(userData.getLeaderId());
        userInfo.setDepartments(userData.getDepartments(), userData.getMainDepartmentId());
        return userInfo;
    }

}
